package com.pactera.miyuangroup.controller.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.pactera.miyuangroup.db.entity.LoginUser;
import com.pactera.miyuangroup.db.entity.VehicleInfo;
import com.pactera.miyuangroup.filter.CurrentUser;
import com.pactera.miyuangroup.service.LoginUserService;
import com.pactera.miyuangroup.service.VehicleInfoService;
import com.pactera.miyuangroup.service.VehicleUserService;
import com.pactera.miyuangroup.service.VendorInfoService;
import com.pactera.miyuangroup.vo.convertor.VehicleInfoConvert;
import com.pactera.miyuangroup.vo.request.PageRequest;
import com.pactera.miyuangroup.vo.request.web.PageDeliverymanAdjustReq;
import com.pactera.miyuangroup.vo.request.web.UpdateDeliverymanAdjustReq;
import com.pactera.miyuangroup.vo.response.ResultModel;
import com.pactera.miyuangroup.vo.response.ResultStatusCode;
import com.pactera.miyuangroup.vo.response.User;
import com.pactera.miyuangroup.vo.response.VoidResponse;
import com.pactera.miyuangroup.vo.response.web.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 配送员调整
 *
 * @author Wangwu
 * @create 2018-04-24 上午11:12
 **/
@Api(description = "配送员调整接口")
@RestController
@RequestMapping("adjust")
public class DeliverymanAdjustController {

    @Autowired
    private VehicleUserService vehicleUserService;
    @Autowired
    private VehicleInfoService vehicleInfoService;
    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private VehicleInfoConvert vehicleInfoConvert;

    /***
     * 配送员调整列表
     * @param page
     * @return
     */
    @ApiOperation(value = "配送员调整列表", notes = "配送员调整列表分页查询")
    @RequestMapping(value = "pageList", method = RequestMethod.POST)
    public ResultModel<Page<PageDeliverymanAdjustRes>> pageList(@CurrentUser User user, @RequestBody PageDeliverymanAdjustReq page) {
        return ResultModel.ok(vehicleInfoService.selectListPage(page, user.getOrgId()));
    }


    /***
     * 配送员调整记录列表
     * @param vehicleId
     * @return
     */
    @ApiOperation(value = "配送员调整记录列表", notes = "配送员调整记录列表分页查询")
    @ApiImplicitParams({@ApiImplicitParam(value = "车辆ID", name = "vehicleId", required = true, paramType = "path")})
    @RequestMapping(value = "record/{vehicleId}", method = RequestMethod.POST)
    public ResultModel<Page<PageDeliverymanAdjustRecordRes>> pageRecordList(@PathVariable String vehicleId, @RequestBody PageRequest req) {
        return ResultModel.ok(vehicleUserService.selectListAdjustRecordPage(vehicleId, req));
    }

    /**
     * 车辆调整信息查询
     *
     * @param vehicleId
     * @return
     */
    @ApiOperation(value = "车辆调整信息查询", notes = "车辆调整信息查询")
    @ApiImplicitParams({@ApiImplicitParam(value = "车辆ID", name = "vehicleId", required = true, paramType = "path")})
    @RequestMapping(value = "/info/{vehicleId}", method = RequestMethod.GET)
    public ResultModel<QueryDeliverymanAdjustRes> getVendorInfo(@PathVariable String vehicleId) {

        VehicleInfo vehicleInfo = vehicleInfoService.selectById(vehicleId);
        if (null == vehicleInfo) {
            ResultModel.fail(ResultStatusCode.TEMPORARILY_NO_DATA);
        }
        LoginUser user = loginUserService.selectById(vehicleInfo.getUserId());
        return ResultModel.ok(vehicleInfoConvert.convert2QueryDeliverymanAdjustRes(vehicleInfo, user));
    }

    /**
     * 车辆人员调整
     *
     * @param vehicleId
     * @return
     */
    @ApiOperation(value = "车辆人员调整", notes = "车辆人员调整")
    @ApiImplicitParams({@ApiImplicitParam(value = "车辆ID", name = "vehicleId", required = true, paramType = "path")})
    @RequestMapping(value = "/{vehicleId}", method = RequestMethod.POST)
    public ResultModel<VoidResponse> adjust(@PathVariable String vehicleId, @CurrentUser User user, @RequestBody UpdateDeliverymanAdjustReq req) {
        boolean flg = vehicleInfoService.vehicleAdjustUser(vehicleId, user.getId(), req);
        if (!flg) {
            return ResultModel.fail();
        }
        return ResultModel.ok();
    }
}
